package com.durablemethods.tsmserver;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.durablemethods.tsmserver.home.TradesHome;
import com.durablemethods.tsmserver.model.Trade;

public class TradeServlet extends HttpServlet {
	private TradesHome tradesHome = new TradesHome();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/plain");
		List<Trade> transactions = tradesHome.getTrades(resp.getWriter());
		SimpleDateFormat sdf= new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
		resp.getWriter().println("Trader,Team, Quanity, Price, Timestamp");
		for (Trade trade : transactions) {
			String format = String.format("%s,%s,%s,%s,%s", trade.getTrader(), trade.getTeam(), trade.getQuanitity(), trade.getPrice(),sdf.format(trade.getTradeTimestamp()));
			resp.getWriter().println(format);
		}
	}

}
